package com.zhongfl.crm.lixiao.bean.resolve;

import com.heptagram.lixiao.bean.response.LXUserResponse;
import com.heptagram.lixiao.bean.response.revisitlog.LXRevisitLogResponse;
import com.zhongfl.crm.service.boss.ChannelRevisitLogService;
import com.zhongfl.crm.model.LxUser;
import com.zhongfl.crm.model.boss.ChannelRevisitLog;
import com.zhongfl.crm.service.LxUserService;
import org.apache.commons.lang3.StringUtils;
import tech.heptagram.channel.api.model.Channel;

import java.util.Objects;

/**
 * ChannelRevisitLogModelResolver 服务类
 *
 * @author tiantian
 * @date 2018/7/12 14:41
 */
public class ChannelRevisitLogModelResolver implements ModelResolver<ChannelRevisitLog, LXRevisitLogResponse.RevisitLog> {

    private Channel channel;

    private ChannelRevisitLogService channelRevisitLogService;

    private LxUserService lxUserService;

    public ChannelRevisitLogModelResolver(Channel channel,LxUserService lxUserService,ChannelRevisitLogService channelRevisitLogService) {
        this.channel = channel;
        this.lxUserService = lxUserService;
        this.channelRevisitLogService = channelRevisitLogService;
    }

    @Override
    public ChannelRevisitLog resolve(LXRevisitLogResponse.RevisitLog revisitLog) throws ModelResolverException {
        if (Objects.isNull(channel)) {
            throw new ModelResolverException("本地渠道记录为空！");
        }

        ChannelRevisitLog channelRevisitLog = new ChannelRevisitLog();
        channelRevisitLog.setChannelId(channel.getId());

        LXUserResponse.User user = revisitLog.getUser();
        LxUser lxUser = lxUserService.findByLxId(user.getId());

        if (Objects.isNull(lxUser)) {
            throw new ModelResolverException("lx用户id: " + user.getId() + " 在用户表找不到");
        }
        if (StringUtils.isEmpty(lxUser.getStaffName())) {
            channelRevisitLog.setStaffName(lxUser.getName());
            channelRevisitLog.setModifiedBy(lxUser.getName());
        } else {
            channelRevisitLog.setStaffName(lxUser.getStaffName());
            channelRevisitLog.setModifiedBy(lxUser.getStaffName());
        }
        channelRevisitLog.setStaffCode(lxUser.getStaffCode());
        channelRevisitLog.setAvatarUrl(user.getAvatarUrl());

        channelRevisitLog.setWay(revisitLog.getCategoryDisplay());
        //channelRevisitLog.setStatus();
        channelRevisitLog.setType("REMARK");
        //channelRevisitLog.setParentId();
        channelRevisitLog.setContent(revisitLog.getContent());
        channelRevisitLog.setOperateTime(revisitLog.getCreateAt());
        //channelRevisitLog.setAtStaffs();
        channelRevisitLog.setAtNoticeSendStatus("SUCCESS_SEND");

        channelRevisitLog.setLxRevisitLogId(revisitLog.getId());
        channelRevisitLog.setLxLoggableId(revisitLog.getLoggableId());
        channelRevisitLog.setLxLoggableType(revisitLog.getLoggableType());

        channelRevisitLog.setSyncStatus("SUCCESS_SYNC");
        //channelRevisitLog.setRetryTimes();

        return channelRevisitLog;
    }

    public ChannelRevisitLog resolveComment(LXRevisitLogResponse.RevisitLog revisitLog, LXRevisitLogResponse.Comment comment) {
        if (Objects.isNull(channel)) {
            throw new ModelResolverException("本地渠道记录为空！");
        }

        ChannelRevisitLog channelRevisitLog = new ChannelRevisitLog();
        channelRevisitLog.setChannelId(channel.getId());

        LXUserResponse.User user = comment.getUser();
        LxUser lxUser = lxUserService.findByLxId(user.getId());

        if (Objects.isNull(lxUser)) {
            throw new ModelResolverException("lx用户id: " + user.getId() + " 在用户表找不到");
        }
        if (StringUtils.isEmpty(lxUser.getStaffName())) {
            channelRevisitLog.setStaffName(lxUser.getName());
            channelRevisitLog.setModifiedBy(lxUser.getName());
        } else {
            channelRevisitLog.setStaffName(lxUser.getStaffName());
            channelRevisitLog.setModifiedBy(lxUser.getStaffName());
        }
        channelRevisitLog.setStaffCode(lxUser.getStaffCode());
        channelRevisitLog.setAvatarUrl(user.getAvatarUrl());

        //channelRevisitLog.setWay(revisitLog.getCategoryDisplay());
        //channelRevisitLog.setStatus();
        channelRevisitLog.setType("COMMENT");
        ChannelRevisitLog lxRevisitLog = channelRevisitLogService.findByLxRevisitLogId(revisitLog.getId());
        channelRevisitLog.setParentId(lxRevisitLog.getId());
        channelRevisitLog.setContent(revisitLog.getContent());
        channelRevisitLog.setOperateTime(revisitLog.getCreateAt());
        //channelRevisitLog.setAtStaffs();
        //channelRevisitLog.setAtNoticeSendStatus("SUCCESS_SEND");

        channelRevisitLog.setLxRevisitLogId(comment.getId());
        channelRevisitLog.setLxLoggableId(revisitLog.getId());
        channelRevisitLog.setLxLoggableType(revisitLog.getLoggableType());

        channelRevisitLog.setSyncStatus("SUCCESS_SYNC");
        //channelRevisitLog.setRetryTimes();
        return channelRevisitLog;
    }
}
